package com.groupseven.backend.controller;

import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.Sidebar.MySidebar;
import com.groupseven.backend.domain.Sidebar.SidebarItems;
import com.groupseven.backend.domain.Sidebar.SidebarMap;
import com.groupseven.backend.domain.Sidebar.SubSidebarMap;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.services.AuthoritiesService;
import com.groupseven.backend.services.UserService;
import com.groupseven.backend.test.DataEncode;
import com.groupseven.backend.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/Sidebar")
public class SidebarController {
    @Autowired
    private AuthoritiesService authoritiesService;
    @Autowired
    private DataEncode dataEncode;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * @Author 王志华
     * @param Username
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @GetMapping("/{Username}")
    public ResultType getSidebar(@PathVariable @Valid String Username) throws NoSuchFieldException, IllegalAccessException {
        User user = new User();
        user.setUsername(Username);
        System.out.println(user.getUsername());
        //System.out.println(user.getPassword());
        //user.setUserid(dataEncode.encodeBae64(user.getUserid()));
        System.out.println(user.getUsername());
        String name = user.getUsername();
        user.setUsername(dataEncode.encodeBae64(user.getUsername()));
        if(userService.selectByUsername(user.getUsername())==null){
            return new ResultType(0,"无此用户",200,null,null,null,null,null);
        }
        MySidebar mysidebar = new MySidebar();
        User u1 = userService.selectByUsername(user.getUsername());
        u1.setUsername(dataEncode.decodeBae64(u1.getUsername()));
        user.setUsername(dataEncode.decodeBae64(user.getUsername()));
        System.out.println(user.getUsername());
        System.out.println("u1 "+u1.getUsertype());
        /**
         * 用户能看到的页面
         */
        if(redisUtil.hasKey(name+"MySidebar")) {
            System.out.println("从redis中获取");
            mysidebar = (MySidebar) redisUtil.get(name+"MySidebar");
        } else {
            System.out.println("从Mysql中获取");
            mysidebar = authoritiesService.getMySidebar(user);
            redisUtil.set(name+"MySidebar",mysidebar,60);
        }
        /**
         * 一级菜单number-to-column映射
         */
        List<SidebarMap> smap = authoritiesService.getSidebarMap();
        /**
         * 二级菜单number-to-column映射
         */
        List<SubSidebarMap> ssmap = authoritiesService.getSubSidebarMap();
        /**
         * 所有的一级菜单项目
         */
        List<SidebarItems> si1 = authoritiesService.getSidebar(u1);
        /**
         * 所有的二级菜单项目
         */
        List<SidebarItems> si2 = authoritiesService.getSubSidebar(u1);
        List<SidebarItems> result = new ArrayList<>();
        List<SidebarItems> subs1 = new ArrayList<>();
        List<SidebarItems> subs2 = new ArrayList<>();
        List<SidebarItems> subs3 = new ArrayList<>();
        List<SidebarItems> subs4 = new ArrayList<>();
        List<SidebarItems> subs5 = new ArrayList<>();
        for(SidebarItems tmpSidebar : si1){
            for(SidebarMap tmpMap : smap){
                if(tmpSidebar.getNumber()==tmpMap.getValue()){
                    String key = tmpMap.getKey();
                    Field field = mysidebar.getClass().getField(key);
                    int my = 0;
                    my = (int)field.get(mysidebar);
                    if(my==1){
                        result.add(tmpSidebar);
                    }
                    break;
                }
            }
            if (tmpSidebar.getTitle().equals("研究成果")){
                for (SidebarItems subsidebar : si2){
                    for (SubSidebarMap tmpssmp : ssmap){
                        if (subsidebar.getFront_number()==tmpSidebar.getNumber()){
                            if(subsidebar.getNumber()==tmpssmp.getValue()){
                                String key = tmpssmp.getKey();
                                Field field = mysidebar.getClass().getField(key);
                                int my = 0;
                                my = (int)field.get(mysidebar);
                                if(my==1){
                                    subs1.add(subsidebar);
                                }
                                break;
                            }

                        }
                    }
                }
                tmpSidebar.setSubs(subs1);
            }
            if (tmpSidebar.getTitle().equals("权限管理")){
                for (SidebarItems subsidebar : si2){
                    for (SubSidebarMap tmpssmp : ssmap){
                        if (subsidebar.getFront_number()==tmpSidebar.getNumber()){
                            if(subsidebar.getNumber()==tmpssmp.getValue()){
                                String key = tmpssmp.getKey();
                                Field field = mysidebar.getClass().getField(key);
                                int my = 0;
                                my = (int)field.get(mysidebar);
                                if(my==1){
                                    subs2.add(subsidebar);
                                }
                                break;
                            }

                        }
                    }
                }
                tmpSidebar.setSubs(subs2);
            }
            if (tmpSidebar.getTitle().equals("日志管理")){
                for (SidebarItems subsidebar : si2){
                    for (SubSidebarMap tmpssmp : ssmap){
                        if (subsidebar.getFront_number()==tmpSidebar.getNumber()){
                            if(subsidebar.getNumber()==tmpssmp.getValue()){
                                String key = tmpssmp.getKey();
                                Field field = mysidebar.getClass().getField(key);
                                int my = 0;
                                my = (int)field.get(mysidebar);
                                if(my==1){
                                    subs3.add(subsidebar);
                                }
                                break;
                            }

                        }
                    }
                }
                tmpSidebar.setSubs(subs3);
            }
            if (tmpSidebar.getTitle().equals("科研考核")){
                for (SidebarItems subsidebar : si2){
                    for (SubSidebarMap tmpssmp : ssmap){
                        if (subsidebar.getFront_number()==tmpSidebar.getNumber()){
                            if(subsidebar.getNumber()==tmpssmp.getValue()){
                                String key = tmpssmp.getKey();
                                Field field = mysidebar.getClass().getField(key);
                                int my = 0;
                                my = (int)field.get(mysidebar);
                                if(my==1){
                                    subs4.add(subsidebar);
                                }
                                break;
                            }

                        }
                    }
                }
                tmpSidebar.setSubs(subs4);
            }
            if (tmpSidebar.getTitle().equals("项目审核")){
                for (SidebarItems subsidebar : si2){
                    for (SubSidebarMap tmpssmp : ssmap){
                        if (subsidebar.getFront_number()==tmpSidebar.getNumber()){
                            if(subsidebar.getNumber()==tmpssmp.getValue()){
                                String key = tmpssmp.getKey();
                                Field field = mysidebar.getClass().getField(key);
                                int my = 0;
                                my = (int)field.get(mysidebar);
                                if(my==1){
                                    subs5.add(subsidebar);
                                }
                                break;
                            }

                        }
                    }
                }
                tmpSidebar.setSubs(subs5);
            }
        }
        if (userService.selectByUsername(dataEncode.encodeBae64(u1.getUsername())).getUsertype()==1){
            System.out.println(u1.getUserid()+"   "+u1.getUsertype());
            return new ResultType(1,"管理员用户认证成功",200,result,null,null,null,null);
        }
        return new ResultType(1,"教师用户认证成功",200,result,null,null,null,null);
    }

    /**
     * @Author 王志华
     * @param mysidebar
     * @return
     */
    @PutMapping("")
    public ResultType modifySidebar(@RequestBody @Valid MySidebar mysidebar){
        if(authoritiesService.modifyMySidebar(mysidebar)>0) {
            redisUtil.del("MySidebar");
            redisUtil.set("MySidebar", mysidebar, 60);
            return new ResultType(1, "修改成功！", 200, null, null, null, mysidebar,null);
        }
        return new ResultType(0, "修改失败！", 200, null, null, null, mysidebar,null);
    }

}
